package com.flyee.controller.tools.sandtable {
	
	import alternativa.engine3d.controllers.*;
	import alternativa.engine3d.core.*;
	import alternativa.engine3d.display.*;
	import alternativa.types.*;
	
	import com.flyee.utils.gs.TweenMax;
	
	import flash.display.*;
	import flash.events.*;
	
	public class EstateService {
		
		public var displayContainer : Sprite = new Sprite();
		
		//声明场景
	    private var scene:Scene3D;
	    //声明视口
	    public var view:View;
	    //声明摄像机
	  	private var camera:Camera3D;
	  	//声明摄像机控制器
	  	private var cameraController:CameraController;
	  	//旋转摄像机对象
  		private var flyCam : Object3D;
  		//鼠标拖拉缓动
  		private var drager : MouseDrager;
  		
  		//楼盘模型
  		public var estate : Estate3D;
		
		public function EstateService(xmlPath : String) {
			//场景保持比例缩放
//		   	stage.scaleMode = StageScaleMode.NO_SCALE;
		   	//场景左上对齐
//		   	stage.align = StageAlign.TOP_LEFT;
		
		   	//调用3DS文件载入函数
		   	estate = new Estate3D(xmlPath);
		   	estate.service = this;
		   	//调用初始化场景函数
		   	initScene();
		   	//添加侦听器
		   	initListeners();
		}
		
		//建立并设置场景和摄像机、摄像机控制器
		private function initScene():void {
	   	  	//创建场景
			scene = new Scene3D();
	   		scene.root = new Object3D();		   
//		   	scene.root.addChild(estate);
		  	//创建摄像机并添加到视口
		   	camera = new Camera3D();
		   	flyCam = new Object3D();
		   	camera.orthographic = true;
		   	camera.x = 1000;
		   	camera.y = 0;
	   		camera.z = 500;
	   		flyCam.addChild(camera);
	   		scene.root.addChild(flyCam);
			//	scene.root.addChild(camera);
	   		view = new View();
//	   		displayContainer.addChild(new Bitmap(new BitmapData(800, 600, false, 0x000000)));
	   		displayContainer.addChild(view);   
	   		view.camera = camera;
	   		view.interactive = true;
	   
	   		//创建并初始化摄像机控制器   
	  		initCameraController();
	  		drager = new MouseDrager(camera,flyCam,cameraController);
//   			drager.stg = this;
	   
	   //显示FPS监视窗口
//	   FPS.init(stage);
	  	}
	  
	  	private function initCameraController() : void {
		  	//创建并初始化摄像机控制器   
		  	cameraController = new CameraController(displayContainer);
		   	cameraController.camera = camera;
		   	cameraController.setDefaultBindings();
		   	cameraController.checkCollisions = false;
		   	cameraController.collisionRadius = 10;
		   	cameraController.controlsEnabled = false;
		   	cameraController.speed = 500;
		   	cameraController.lookAt(new Point3D(0, 0, 0));
  		}
  		
  		//添加侦听器函数
	  	private function initListeners():void{
//	  		estate.addEventListener(Event.COMPLETE, estateCompleteHandle);
	   		displayContainer.addEventListener(Event.RESIZE, onResize);
	   		displayContainer.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
	   		displayContainer.addEventListener(MouseEvent.MOUSE_DOWN, drager.onMouseClick);
	    	displayContainer.addEventListener(MouseEvent.MOUSE_UP, drager.onMouseClick);
	    	displayContainer.addEventListener(MouseEvent.MOUSE_MOVE, drager.onMouseClick);
	    	displayContainer.addEventListener(MouseEvent.MOUSE_OUT, drager.onMouseClick);
//    		displayContainer.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
	  	}
	  	
	  	private function removeListeners() : void {
	  		displayContainer.removeEventListener(Event.RESIZE, onResize);
	   		displayContainer.removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
	   		displayContainer.removeEventListener(MouseEvent.MOUSE_DOWN, drager.onMouseClick);
	    	displayContainer.removeEventListener(MouseEvent.MOUSE_UP, drager.onMouseClick);
	    	displayContainer.removeEventListener(MouseEvent.MOUSE_MOVE, drager.onMouseClick);
	    	displayContainer.removeEventListener(MouseEvent.MOUSE_OUT, drager.onMouseClick);
//    		displayContainer.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
	  	}
	  	
	  	private function onMouseWheel(e:MouseEvent):void {
//	  		if(estate.configCompleted)
//				multiplyZoom(e.delta/20);
		}

		private function multiplyZoom(value:Number):void {
			var zoom:Number = camera.zoom*(1 + value);
			camera.zoom = (zoom < 0.1) ? 0.1 : (zoom > 20 ? 20 : zoom);
			if(camera.zoom<0.1)
				camera.zoom=0.1;
			else if(camera.zoom>0.5)
				camera.zoom=0.5;
//			estate.modifySignScale((1-(camera.zoom-0.1)/(0.5-0.1))*3+1);
		}
	  	
	  	private function onResize(e:Event):void{
   			//当FLASH大小变动的时候，视口的尺寸也随之变化
//   			view.width = stage.stageWidth; 
//   			view.height = stage.stageHeight;
				view.width = 640;
   				view.height = 480;
  		}
  		
  		public function afterEstateLoad(arr : Array) : void {
  			drager.objs = arr;
  			scene.root.addChild(estate);
//  			flyCam.rotationZ = 90;
  			view.width = 640;
   			view.height = 480;
   			camera.zoom = 0.4;
  		}
  		
  		private function OnEnterFrame(e:Event):void{
		   //处理用户输入,既处理对用户对视口变化的操作
		   //可以在视口中拖拽鼠标移动视口方向，可以通过滚轮进行缩放
		   //	cameraController.processInput();
//			estate.estate3DHandle();
	       //缓动处理
	       	drager.process();

   			//场景渲染
   			try{
	   			if(estate.configCompleted)
	   				scene.calculate();
   			}catch(err : Error) {}
		   //一开始object是没有初始化的，所以加上这句防止出错
//		   if (object) {
		    //让3D对象沿Y轴旋转
		//    object.rotationZ = getTimer() / 400;
//		   	}
  		}
  		
  		public function zoomControl(num : Number, scale : Number) : void {
//  			camera.zoom = num;
  			TweenMax.to(camera, 2, {zoom:num, delay:0});
  			estate.modifySignScale(scale);
  		}
  		
  		public function estateCompleteHandle(event : Event) : void {
  			
  		}
  		
  		public function dispose() : void {
  			removeListeners();
  			drager.dispose();
  			estate.dispose();
  			scene.root.removeChild(estate);
  			estate = null;
  			flyCam.removeChild(camera);
  			cameraController.camera = null;
  			camera.view = null;
  			camera = null;
  			scene.root.removeChild(flyCam);
  			flyCam = null;
  			scene.root = null;
  			view.camera = null;
  			displayContainer.removeChild(view);
  			//view的dispose()待定
  			view = null;  			
  			displayContainer = null;
  			
  		}

	}
}